
app.directive('pageControl', function () {
    return {
        restrict: 'AE',
        scope: {
            totalNum: '=',
            pageOptions: '='
        },
        replace: true,
        link: function (scope, element, attrs) {
            var defaultPageOptions = {
                num_edge_entries: 2,//两侧显示的首尾分页的条目数
                num_display_entries: 6,//连续分页主体部分显示的分页条目数
                items_per_page: 10,//每页显示的条目数
                prev_text: "上一页",
                next_text: "下一页"
            };
            var pageNum = scope[attrs.totalNum] || 0,
                pageOptions = $.extend(defaultPageOptions, scope[attrs.pageOptions]),
                page;
            scope.$watch('totalNum', function (newVal) {
                if (newVal && newVal != pageNum) {
                    pageNum = newVal;
					page = $(element).pagination(pageNum, pageOptions).get(0);
//                   	page.refresh();
                }
            }, true);
            scope.$watch('pageOptions', function (newVal) {
                if (newVal && newVal != pageOptions) {
                    pageOptions = $.extend(pageOptions, newVal);
                    if(pageNum == 1) {
						page = $(element).pagination(pageNum, pageOptions).get(0);
					}
                }
            }, true);
        }
    }
});

/**
 *小分页*/
app.directive('smpage', function () {
    return {
        restrict: 'AE',
        scope: {
            totalNum: '=',
            pageOptions: '='
        },
        template: '<div class="bbs-pag">' +
        '<a href="javascript:;" style="margin-right:5px" ng-click="pageByIndex($index+1)" ng-repeat="item in pages" ng-class="{\'cur\': curPage==$index+1}">{{$index+1}}</a>' +
        '<a href="javascript:;" ng-click="nextPage()" ng-if="pageNum>0">下一页</a>' +
        '<a href="javascript:;" style="margin-left: 5px" ng-click="endPage()" ng-if="pageNum>0">尾页</a>' +
        '</div>',
        replace: true,
        link: function (scope, element, attrs) {
            var defaultOptions = {
                    pageSize: 5,
                    callback: angular.noop
                },
                options = angular.extend(defaultOptions, scope[attrs.pageOptions] || {});
            function refresh(totalNum) {
                //计算总页数
                var pageNum = Math.ceil(totalNum / options.pageSize),
                    temp = [];
                for (var i = 0; i < pageNum; ++i) {
                    temp.push(i + 1);
                }
                scope.curPage = 1;
                scope.pages = temp;
                scope.pageNum = pageNum;

            }

            scope.pageByIndex = function (index) {
                if (scope.curPage == index) return;
                options.callback(index);
                scope.curPage = index;
            }
            scope.nextPage = function () {
                if (scope.curPage == scope.pageNum) return;
                options.callback(scope.curPage + 1);
                scope.curPage = scope.curPage + 1;
            }
            scope.endPage = function () {
                if (scope.curPage == scope.pageNum) return;
                options.callback(scope.pageNum);
                scope.curPage = scope.pageNum;
            }
            scope.$watch('totalNum', function (newVal) {
                if (newVal) {
                    refresh(newVal);
                }
            }, true);
            scope.$watch('pageOptions', function (newVal) {
                //console.log(newVal)
                if (typeof(newVal) =="object" && newVal != defaultOptions) {
                    options = angular.extend(defaultOptions, newVal);
                }
            }, true);

        }
    }
});
